package com.account.book.common.exception;

import com.account.book.common.response.BaseResult;
import com.account.book.common.response.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;

/**
 * @author liang-hw
 * @className GlobalExceptionHandler
 * @description
 * @date 2023/11/09 15:11:14
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {


    /**
     * @param @param ex
     * @return @return {@link BaseResult }
     * @author liang-hw
     * @description 处理基本信息相关的异常
     * @date 2023/11/09 15:11:32
     */
    @ExceptionHandler(BasicInfoException.class)
    @ResponseBody
    public BaseResult handleBasicInfoException(BasicInfoException ex) {
        return ResultUtils.error(ex.getCode(), ex.getMessage());
    }


    /**
     * @param @param ex
     * @return @return {@link BaseResult }
     * @author liang-hw
     * @description 处理未知异常
     * @date 2023/11/09 15:11:37
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public BaseResult handleUnKnowException(Exception ex) {
        return ResultUtils.error(BasicInfoStatusEnum.UNKNOWN_ERROR.getCode(), ex.getMessage());
    }


    /**
     * @param @param exception
     * @return @return {@link Object }
     * @author liang-hw
     * @description 文件太大异常
     * @date 2023/11/09 15:11:42
     */
    @ResponseBody
    @ExceptionHandler(value = MultipartException.class)
    public Object fileUploadExceptionHandler(MultipartException exception) {
        return ResultUtils.error(BasicInfoStatusEnum.FILE_MAN_ERROR.getCode(), BasicInfoStatusEnum.FILE_MAN_ERROR.getDesc());
    }
}
